package com.wzx.ptplatform.controller.admin.student;

import com.wzx.ptplatform.domain.CollectMessage;
import com.wzx.ptplatform.domain.Student;
import com.wzx.ptplatform.service.CollectMessageService;
import com.wzx.ptplatform.utils.LayuiPager;
import com.wzx.ptplatform.utils.LayuiUtils;
import com.wzx.ptplatform.utils.MapUtils;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author 紫煦桉
 */
@Api(tags = "管理员学生模块——收藏兼职信息管理")
@RestController
public class AdminStudentStudentCollectController {

    final CollectMessageService collectMessageService;

    public AdminStudentStudentCollectController(CollectMessageService collectMessageService) {
        this.collectMessageService = collectMessageService;
    }

    @RequestMapping("admin_student_student_collect_message_list")
    public LayuiPager<CollectMessage> adminStudentStudentCollectMessageList(
            String studentName,
            @RequestParam(defaultValue = "1", value = "page") Integer page,
            @RequestParam(defaultValue = "10", value = "limit") Integer limit) {
        CollectMessage collectMessage = new CollectMessage();
        if (studentName != null && !"".equals(studentName)) {
            collectMessage.setStudent(new Student(studentName));
        }
        Page<CollectMessage> pager = collectMessageService.findList(collectMessage, page - 1, limit);
        LayuiPager<CollectMessage> layuiPager = LayuiUtils.initPager(page, limit);
        LayuiUtils.setPager(layuiPager, pager.getContent(), Math.toIntExact(pager.getTotalElements()));
        return layuiPager;
    }

    @GetMapping("admin_student_student_collect_message_addView")
    public ModelAndView adminStudentStudentCollectMessageAddView() {
        return new ModelAndView("admin/student/student_collect_message_add");
    }

    @GetMapping("admin_student_student_collect_message_editView")
    public ModelAndView adminStudentStudentCollectMessageEditView(String id, Model model) {
        model.addAttribute("collectMessage", collectMessageService.findById(Long.parseLong(id)));
        return new ModelAndView("admin/student/student_collect_message_edit");
    }

    @PostMapping("admin_student_student_collect_message_save_or_edit")
    public Map<String, Object> adminStudentCollectMessageSaveOrEdit(CollectMessage collectMessage) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (collectMessageService.saveOrEdit(collectMessage)) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_student_collect_message_delete")
    public Map<String, Object> adminStudentStudentCollectMessageDeleted(String id) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (collectMessageService.deleteById(Long.parseLong(id))) {
            MapUtils.successMap(map);
        }
        return map;
    }

    @PostMapping("admin_student_student_collect_message_delete_select")
    public Map<String, Object> adminStudentStudentCollectMessageDeletedSelect(String arrayId) {
        Map<String, Object> map = MapUtils.initializeMap();
        if (arrayId != null && !"".equals(arrayId)) {
            String[] array = arrayId.split(",");
            for (String id : array) {
                if (id != null && !"".equals(id)) {
                    collectMessageService.deleteById(Long.parseLong(id));
                }
            }
            MapUtils.successMap(map);
        }
        return map;
    }
}
